package com.ewili.x.special.service.data.impl;

import com.ewili.framework.utils.HtmlUnitUtils;
import com.ewili.framework.utils.ObjectHttpClientUtils;
import com.ewili.framework.utils.StringUtils;
import com.ewili.x.special.dao.inter.StockDao;
import com.ewili.x.special.dao.inter.TradableHolderDao;
import com.ewili.x.special.entity.Stock;
import com.ewili.x.special.service.data.inter.TradableShareHoldersService;
import com.ewili.x.special.task.thread.inter.TradableHolderThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class TradableShareHoldersServiceImpl implements TradableShareHoldersService {

    @Autowired
    private StockDao stockDao;

    @Autowired
    private TradableHolderDao tradableHolderDao;

    @Resource
    private TradableHolderThreadTask tradableHolderThreadTask;

    @Override
    public void spide163TradableShareHolder() {
        List<Stock> stocks = stockDao.findAll();
        for (Stock stock : stocks) {
            String code = stock.getCode();
            tradableHolderThreadTask.spideTradableHolder(code);
        }
    }

    @Override
    public void spideEastMoneyTradableHolder() {
        List<Stock> stocks = stockDao.findAll();
        for (Stock stock : stocks) {
            String code = stock.getCode();
            String market = stock.getMarket();
            this.validEastMoneyDate(code);
            try {
                tradableHolderThreadTask.spideEastMoneyTradableHolder(market, code);
            } catch (Exception e) {
                String url = "http://f10.eastmoney.com/ShareholderResearch/ShareholderResearchAjax?code=" + market + code;
                log.error("url:{}", url, e);
            }

        }
    }

    @Override
    public void validEastMoneyDate(String code) {

    }

    @Override
    public void spideEastMoneyTradableShareHolders() {
        String content = HtmlUnitUtils.getContent("http://data.eastmoney.com/gdfx/HoldingDetail.aspx");
        Document document = Jsoup.parse(content);
        List<String> dataValues = document.getElementsByAttribute("data-value").eachAttr("data-value");
        for (String dataValue : dataValues) {
            if (StringUtils.isBlank(dataValue)) {
                continue;
            }

            String baseUrl = "http://data.eastmoney.com/DataCenter_V3/gdfx/data.ashx?SortType=NDATE&SortRule=1&PageIndex=1&PageSize=50&jsObj=rwNYJPLE&type=NSHDDETAIL&date=" + dataValue + "&gdlx=0&cgbd=0&rt=52575177";
            Long pages = ObjectHttpClientUtils.getEastMoneyJsonPages(baseUrl);
            for (int i = 1; i <= pages; i++) {
                String url = "http://data.eastmoney.com/DataCenter_V3/gdfx/data.ashx?SortType=NDATE&SortRule=1&PageIndex=" + i + "&PageSize=50&jsObj=rwNYJPLE&type=NSHDDETAIL&date=" + dataValue + "&gdlx=0&cgbd=0&rt=52575177";
                System.out.println(pages);
            }

        }


    }

}
